package com.zust.ysc012.service;

import com.zust.ysc012.entity.Project;
import com.zust.ysc012.entity.Table;
import javafx.scene.control.Tab;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.zust.ysc012.dao.PublicDao.select_all_project_sql;
import static com.zust.ysc012.dao.PublicDao.select_project_name;
import static com.zust.ysc012.dao.StudentDao.*;
import static com.zust.ysc012.dao.TeacherDao.select_teacher_name_sql;


/**
 * @Description
 * @Author Mato
 * @Date 03/07/2021 09:24
 */
public class UploadedService {
    public static List<Project> getAllTable() throws SQLException {
        List<Project> projects = new ArrayList<>();
        List<Table> tables = new ArrayList<>();
        String teacher_number;
        String name;
        projects = select_all_project_sql();
        for (int i = 0; i < projects.size(); i++) {
            teacher_number = projects.get(i).getTeacher_number();
            name = select_teacher_name_sql(teacher_number);
            projects.get(i).setTeacher_number(name);
        }

        return projects;
    }

    public static List<Table> getAllTable2() throws SQLException {
        List<Project> projects = new ArrayList<>();
        List<Table> tables = new ArrayList<>();
        projects = getAllTable();
//        System.out.println(projects);
        for (int i = 0; i < projects.size(); i++) {
            Table table = new Table();
            table.setProject_name(projects.get(i).getName());
            table.setProject_number(projects.get(i).getProject_number());
            table.setStatus(select_status_sql(projects.get(i).getProject_number()));
//            table.setStudent_name(select_student_name_sql(rs.getString(2)));
//            table.setStudent_number(rs.getString(2));
            table.setTeacher_name(select_teacher_name_sql(projects.get(i).getTeacher_number()));
            table.setTeacher_number(projects.get(i).getTeacher_number());
            table.setStarting_time(projects.get(i).getStarting_time());
            table.setDeadline(projects.get(i).getDeadline());
            tables.add(table);
        }
//        System.out.println(tables);
        return tables;
    }

    public static void insert_student_project(int project_number, String student_number) throws SQLException {
        List<Project> projects = new ArrayList<>();
        String teacher_number = null;
        projects = select_all_project_sql();
        for (int i = 0; i < projects.size(); i++) {

            if (project_number == projects.get(i).getProject_number()) {
                teacher_number = projects.get(i).getTeacher_number();
                break;
            }
        }
        insert_student_project_sql(student_number, project_number, teacher_number);

    }
}
